package com.wish.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.wish.utils.Utils;

@Entity
@Table(name = "t_user")
public class User implements Serializable
{

	private static final long serialVersionUID = 1946565865390558320L;

	@Id
	@GeneratedValue
	@Column(name = "ID", nullable = false)
	private long id = 0;

	@Column(name = "UserName", length = 40, nullable = false, unique = true)
	private String userName = "";

	@Column(name = "Password", length = 40, nullable = false)
	private String password = "";

	@Column(name = "RealName", length = 40, nullable = false)
	private String realName = "";

	@Column(name = "Enabled", nullable = false, columnDefinition = "bit")
	private boolean enabled = true;

	@Column(name = "CreateDate", length = 19, nullable = false)
	private Date createDate = Utils.getCurrentDateTimeDate();

	@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinTable(name = "t_user_permission", joinColumns = { @JoinColumn(name = "UserID", referencedColumnName = "ID") }, inverseJoinColumns = {
			@JoinColumn(name = "Permission", referencedColumnName = "Permission") })
	private List<Permission> permissions = new ArrayList<Permission>();

	@ManyToOne(cascade = CascadeType.REFRESH, optional = false)
	@JoinColumn(name = "GroupID", nullable = false)
	private Group group;

	public String getUserName()
	{
		return userName;
	}

	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getRealName()
	{
		return realName;
	}

	public void setRealName(String realName)
	{
		this.realName = realName;
	}

	public boolean isEnabled()
	{
		return enabled;
	}

	public void setEnabled(boolean enabled)
	{
		this.enabled = enabled;
	}

	public List<Permission> getPermissions()
	{
		return permissions;
	}

	public void setPermissions(List<Permission> permissions)
	{
		this.permissions = permissions;
	}

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public Date getCreateDate()
	{
		return createDate;
	}

	public void setCreateDate(Date createDate)
	{
		this.createDate = createDate;
	}

	public boolean ifAnyGranted(String... args)
	{
		List<String> params = Arrays.asList(args);
		for (Permission p : getPermissions())
		{
			if (params.contains(p.getPermission()))
			{
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public Group getGroup()
	{
		return group;
	}

	public void setGroup(Group group)
	{
		this.group = group;
	}

}
